Contents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <cstdlib>
using namespace std;

int n;

struct node{
int date;
int sorts;
}a[1000]; //尽量不用malloc来建动态链表,容易忘了free,直接开一个结构体数组

int cmp(const void *a, const void *b)
{

return ((struct node *)a)->date - ((struct node *)b)->date;//按node里的date来从小到大排序
}//return (*(struct node *)a).date - (*(struct node *)b).date;这样也可以

int finds(struct node *a, int k)
{

qsort(a, n, sizeof(struct node), cmp);
int x;
int l = 0, r = n - 1;
while(l <= r){
x = (l + r) / 2;
if(a[x].date < k)
l = x + 1;
else if(a[x].date > k)
r = x - 1;
else
break;
}
if(l <= r)
return a[x].sorts + 1;
else
return -1;
}

int main()
{

int i, m;
while(scanf("%d", &n) != EOF){
for(i = 0; i < n; i++){
scanf("%d", &a[i].date);
a[i].sorts = i;
}
int t;
scanf("%d", &t);
while(t--){
scanf("%d", &m);
printf("%d\n", finds(a, m)); //输出自然数序号
}
}
return 0;
}
Contents